package com.cardone.generator.script;

import com.cardone.common.template.support.TemplateSupport;
import com.cardone.context.ContextHolder;
import com.cardone.context.action.Run0Action;
import com.cardone.context.action.Run1Action;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.task.TaskExecutor;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by yaohaitao on 15-1-12.
 */
@Slf4j
public class GeneratorRedundancyTriggerScript implements GeneratorScript {
    @Setter
    private TemplateSupport templateSupport;

    /**
     * 文件位置
     */
    @Setter
    private Resource saveLocation;

    @Setter
    private Run1Action<File> savedAction;

    @Override
    public void run() {
        Map<String, GeneratorRedundancyTriggerScriptConfig> generatorRedundancyTriggerScriptConfigMap = ContextHolder.getApplicationContext().getBeansOfType(GeneratorRedundancyTriggerScriptConfig.class);

        if (MapUtils.isEmpty(generatorRedundancyTriggerScriptConfigMap)) {
            return;
        }

        try {
            FileUtils.deleteQuietly(saveLocation.getFile());

            for (Map.Entry<String, GeneratorRedundancyTriggerScriptConfig> generatorRedundancyTriggerScriptConfigEntry : generatorRedundancyTriggerScriptConfigMap.entrySet()) {
                if (CollectionUtils.isEmpty(generatorRedundancyTriggerScriptConfigEntry.getValue().getTemplateFiles())) {
                    continue;
                }

                Map<String, Object> model = Maps.newHashMap();

                model.put("updateTableName", generatorRedundancyTriggerScriptConfigEntry.getValue().getUpdateTableName());

                List<Map<String, String>> newMapList = Lists.newArrayList();

                for (Map<String, String> map : generatorRedundancyTriggerScriptConfigEntry.getValue().getMapList()) {
                    Map<String, String> newMap = Maps.newHashMap();

                    if (MapUtils.isNotEmpty(generatorRedundancyTriggerScriptConfigEntry.getValue().getDefaultMap())) {
                        newMap.putAll(generatorRedundancyTriggerScriptConfigEntry.getValue().getDefaultMap());
                    }

                    newMap.putAll(map);

                    newMapList.add(newMap);
                }

                model.put("newMapList", newMapList);

                for (String templateFile : generatorRedundancyTriggerScriptConfigEntry.getValue().getTemplateFiles()) {
                    String scriptStr = templateSupport.process(templateFile, false, model);

                    FileUtils.writeStringToFile(saveLocation.getFile(), scriptStr, Charsets.UTF_8, true);
                }
            }

            if (savedAction != null) {
                savedAction.run(saveLocation.getFile());
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
